<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="一致性hash用途解决：分布式缓存问题，分库分表等 一致性hash特性(一致性hash存放动态变化的Cache) 平衡性：hash的结果能够尽可能分布到所有的缓存中去 单调性：已经分配了 分散性：相同的内容被不同的终端映射到不同的缓冲区，应降低分散性 负载：降低缓冲的负荷  缓存问题：例子：随着流量增大，服务器的压力越来越大，现在有3台redis缓存 0，1，2最基本的操作：将一次redis请求随">
<meta property="og:type" content="article">
<meta property="og:title" content="一致性hash">
<meta property="og:url" content="http://yoursite.com/2017/09/02/java/一致性hash/index.html">
<meta property="og:site_name" content="Tissan">
<meta property="og:description" content="一致性hash用途解决：分布式缓存问题，分库分表等 一致性hash特性(一致性hash存放动态变化的Cache) 平衡性：hash的结果能够尽可能分布到所有的缓存中去 单调性：已经分配了 分散性：相同的内容被不同的终端映射到不同的缓冲区，应降低分散性 负载：降低缓冲的负荷  缓存问题：例子：随着流量增大，服务器的压力越来越大，现在有3台redis缓存 0，1，2最基本的操作：将一次redis请求随">
<meta property="og:updated_time" content="2017-12-18T01:01:03.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一致性hash">
<meta name="twitter:description" content="一致性hash用途解决：分布式缓存问题，分库分表等 一致性hash特性(一致性hash存放动态变化的Cache) 平衡性：hash的结果能够尽可能分布到所有的缓存中去 单调性：已经分配了 分散性：相同的内容被不同的终端映射到不同的缓冲区，应降低分散性 负载：降低缓冲的负荷  缓存问题：例子：随着流量增大，服务器的压力越来越大，现在有3台redis缓存 0，1，2最基本的操作：将一次redis请求随">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 'undefined',
      author: 'Java程序员'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/09/02/java/一致性hash/"/>





  <title>一致性hash | Tissan</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tissan</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/09/02/java/一致性hash/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="罗天翔">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tissan">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">一致性hash</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-02T18:12:09+08:00">
                2017-09-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index">
                    <span itemprop="name">java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/2017/09/02/java/一致性hash/#SOHUCS" itemprop="discussionUrl">
                  <span id="changyan_count_unit" class="post-comments-count hc-comment-count" data-xid="2017/09/02/java/一致性hash/" itemprop="commentsCount"></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="一致性hash用途"><a href="#一致性hash用途" class="headerlink" title="一致性hash用途"></a>一致性hash用途</h3><p>解决：分布式缓存问题，分库分表等</p>
<h3 id="一致性hash特性-一致性hash存放动态变化的Cache"><a href="#一致性hash特性-一致性hash存放动态变化的Cache" class="headerlink" title="一致性hash特性(一致性hash存放动态变化的Cache)"></a>一致性hash特性(一致性hash存放动态变化的Cache)</h3><ul>
<li>平衡性：hash的结果能够尽可能分布到所有的缓存中去</li>
<li>单调性：已经分配了</li>
<li>分散性：相同的内容被不同的终端映射到不同的缓冲区，应降低分散性</li>
<li>负载：降低缓冲的负荷</li>
</ul>
<h4 id="缓存问题："><a href="#缓存问题：" class="headerlink" title="缓存问题："></a>缓存问题：</h4><p><strong>例子</strong>：随着流量增大，服务器的压力越来越大，现在有3台redis缓存 0，1，2<br>最基本的操作：将一次redis请求随机发送到某台redis，同一份数据在不同的缓存上造成数据冗余；有的机器已经缓存了数据，但是请求到了其他的服务器，没有命中<br>优化：相同的key被发送到相同的服务器</p>
<ul>
<li>如何做到：<br>初级优化：h = Hash(key)%N<ul>
<li>这样相同的key就能访问到相同的服务器</li>
<li>缺点：如果新增一台机器的话，以前的缓存就不能命中了<br>高级优化：将整个hash空间组织成一个虚拟的环 0-2^32-1，</li>
<li>将每个服务器进行Hash后，根据ip或者主机名来hash这样机器的位置就固定了，然后对用户id使用同样的hash也能得到一个数值h,然后按顺时针方向走，先走到哪个服务器就算哪个,如果有一台宕机了，影响的数据量非常小,具有较好的容错性和可扩展性非常好</li>
<li>缺点：如果只有两台服务器离的很近，可能造成数据倾斜，都存放在一台上面<ul>
<li>：引入虚拟节点：假设在其他位置上存在这个机器的虚拟节点这样就可以是缓存能够有较好的平衡性</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="一致性hash比较"><a href="#一致性hash比较" class="headerlink" title="一致性hash比较"></a>一致性hash比较</h3><table>
<thead>
<tr>
<th>keyhash</th>
<th>一致性hash</th>
<th>一致性hash+虚拟节点</th>
</tr>
</thead>
<tbody>
<tr>
<td>hash(object)%N</td>
<td>2^32次方个桶的空间</td>
<td>在2^32次方个桶的空间中增加虚拟节点</td>
</tr>
<tr>
<td>简单易用</td>
<td>如果增、删节点能保证大部分的缓存依旧命中，可扩展性强，容错性强，</td>
<td>更好的体现了一致性hash的平衡型</td>
</tr>
<tr>
<td>如果增、删机器会导致缓存命中急聚降低</td>
<td>如果机器很少可能导致机器分布集中缓存失去平衡型</td>
<td></td>
</tr>
</tbody>
</table>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/uploads/wechat.png" alt="罗天翔 WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="/uploads/ali.jpg" alt="罗天翔 Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/01/mysql/事务/" rel="next" title="mysql innodb事务">
                <i class="fa fa-chevron-left"></i> mysql innodb事务
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/12/java/几种常见排序比较/" rel="prev" title="几种常见排序比较">
                几种常见排序比较 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="SOHUCS"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="罗天翔" />
          <p class="site-author-name" itemprop="name">罗天翔</p>
           
              <p class="site-description motion-element" itemprop="description">一次就把事情做对.</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/louiegit/" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="" target="_blank" title="微信">
                  
                    <i class="fa fa-fw fa-wechat"></i>
                  
                  微信
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一致性hash用途"><span class="nav-number">1.</span> <span class="nav-text">一致性hash用途</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一致性hash特性-一致性hash存放动态变化的Cache"><span class="nav-number">2.</span> <span class="nav-text">一致性hash特性(一致性hash存放动态变化的Cache)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#缓存问题："><span class="nav-number">2.1.</span> <span class="nav-text">缓存问题：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#一致性hash比较"><span class="nav-number">3.</span> <span class="nav-text">一致性hash比较</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">罗天翔</span>
</div>


<div class="powered-by">
  NBA & Code. What?
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  




  
    <script type="text/javascript">
    (function(){
      var appid = 'cyt9ceIwz';
      var conf = 'c6226e2bfb7e9c65efc235736edc5074';
      var width = window.innerWidth || document.documentElement.clientWidth;
      if (width < 960) {
      window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){
        window.changyan.api.config({appid:appid,conf:conf})});
      }
    })();
    </script>
    <script type="text/javascript" src="https://assets.changyan.sohu.com/upload/plugins/plugins.count.js"></script>
  




  





  

  

  

  

  

  

</body>
</html>
